﻿//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Resource Application Block
//===============================================================================

using System;
using System.Diagnostics.CodeAnalysis;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Design;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design.Validation;
using EntLibContrib.Resource.Properties;

namespace EntLibContrib.Resource.Configuration.Design
{
	/// <summary>
	/// Represents a custom resource manager that consists of key value pairs. 
	/// </summary>
	public class CustomResourceManagerNode : ResourceManagerBaseNode
	{
		#region Fields
		private List<EditableKeyValue> editableAttributes = new List<EditableKeyValue>();
		private string typeName;
		#endregion

		#region Properties
		/// <summary>
		/// Gets or sets the attributes for the custom resource manager.
		/// </summary>
		/// <value>
		/// The attributes for the custom resource manager.
		/// </value>		
		[SuppressMessage("Microsoft.Design", "CA1002:DoNotExposeGenericLists")]
		[CustomAttributesValidation]
		[ResourceCategory("CategoryGeneral")]
		[ResourceDescription("CustomResourceManagerExtensionsDescription")]
		public List<EditableKeyValue> Attributes
		{
			get
			{
				return editableAttributes;
			}
		}

		/// <summary>
		/// Gets or sets the <see cref="Type"/> that implements <see cref="IResourceProvider"/>.
		/// </summary>
		/// <value>
		/// The <see cref="Type"/> that implements <see cref="IResourceProvider"/>.
		/// </value>
		[Required]
		[Editor(typeof(TypeSelectorEditor), typeof(UITypeEditor))]
		[BaseType(typeof(IResourceManager), typeof(CustomResourceManagerData))]
		[ResourceCategory("CategoryGeneral")]
		[ResourceDescription("CustomResourceManagerNodeTypeDescription")]
		public string TypeName
		{
			get { return typeName; }
			set { typeName = value; }
		}

		/// <summary>
		/// Gets a <see cref="ResourceManagerDataBase"/> configuration object using the node data.
		/// </summary>
		/// <value>
		/// A <see cref="ResourceManagerDataBase"/> configuration object using the node data.
		/// </value>
		[ReadOnly(true)]
		public override ResourceManagerDataBase ResourceManagerData
		{
			get
			{
				CustomResourceManagerData customResourceManagerData = new CustomResourceManagerData(Name, typeName);
				customResourceManagerData.Attributes.Clear();
				foreach (EditableKeyValue kv in editableAttributes)
				{
					customResourceManagerData.Attributes.Add(kv.Key, kv.Value);
				}
				return customResourceManagerData;
			}
		}
		#endregion

		#region Construction
		/// <summary>
		/// Initialize a new instance of the <see cref="CustomResourceManagerNode"/> class.
		/// </summary>
		public CustomResourceManagerNode()
			: this(new CustomResourceManagerData(InternalResources.DefaultResourceManagerName, string.Empty))
		{
		}

		/// <summary>
		/// Initialize a new instance of the <see cref="CustomResourceManagerNode"/> class with a <see cref="CustomResourceManagerData"/> object.
		/// </summary>
		/// <param name="customResourceManagerData">The <see cref="CustomResourceManagerData"/> to display.</param>
		public CustomResourceManagerNode(CustomResourceManagerData customResourceManagerData)
		{
			if (null == customResourceManagerData) throw new ArgumentNullException("customResourceManagerData");

			Rename(customResourceManagerData.Name);
			foreach (string key in customResourceManagerData.Attributes)
			{
				editableAttributes.Add(new EditableKeyValue(key, customResourceManagerData.Attributes[key]));
			}
			this.typeName = customResourceManagerData.TypeName;
		}
		#endregion
	}
}
